package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> findByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        //分页查询
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        Page<NursingProject> nursingProjects = nursingProjectMapper.findByPage(name, status);
        // 封装
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        // 返回
        return projectVoPageResponse;
    }

    @Override
    public void createNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.createNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo findById(Long id) {
        NursingProjectVo nursingProjectvo = nursingProjectMapper.findById(id);
        return nursingProjectvo;
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingProjectVo vo = nursingProjectMapper.findById(id);
        if (vo == null) {
            throw new RuntimeException("该护理项目不存在");
        }
        NursingProject nursingProject = BeanUtil.toBean(vo, NursingProject.class);
        nursingProject.setStatus(status);
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.dedeleteNursingProject(id);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProjectVo> nursingProjectVoResponseResult = nursingProjectMapper.findAll();
        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(nursingProjectVoResponseResult, NursingProjectVo.class);
        return nursingProjectVos;
    }

}
